package com.education.basic.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.education.basic.entity.Administrator;
import com.education.basic.entity.Teacher;
import com.education.basic.exception.AuthorityException;
import com.education.basic.service.TeacherService;
import com.yunshangwoniu.core.cache.CacheAble;
import com.yunshangwoniu.core.utils.RequestUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.education.basic.entity.TeachInfo;
import com.education.basic.resource.Courses;
import com.education.basic.service.TeachInfoService;
import com.yunshangwoniu.core.baseAction.BaseAction;
import com.yunshangwoniu.core.page.Page;

@Controller
@Scope("prototype")
@Namespace("/teachInfo")
@ParentPackage(value="struts-default")
public class TeachInfoAction extends BaseAction
{
	private static final long serialVersionUID = -7016992798323663186L;
	
	@Autowired
	private TeachInfoService teachInfoService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CacheAble<String,Object> cacheAble;

	private Integer pageNo;	
	private Page<TeachInfo> page;
	private List<Courses> courses;
    private List<Teacher> teachers;
    private String couserName;
    private String couserCode;
    private String teacherName;
    private String teacherCode;

	
	@Action(value="listAssign",results={@Result(name="success",location="list.jsp")})
	public String listAssign()
	{
        pageNo = pageNo==null?1:pageNo;
        Map<String,Object> params = new HashMap<String,Object>();
		page = teachInfoService.queryTeachInfoWhenExsit(pageNo, 10, params, getRequest());
		return "success";
	}

    @Action(value="listNotAssign",results={@Result(name="success",location="list2.jsp")})
	public String listNotAssign()
	{
        teachers = teacherService.queryTeachers();
		courses = teachInfoService.queryTeachInfoWhenNoExsit();
		return "success";
	}

    @Action(value="assign",results={@Result(name="success",location="../inc/message.jsp")})
    public String assign()
    {
        Administrator ad = (Administrator) cacheAble.get(RequestUtils.getIp(getRequest()));
        if(ad==null||(ad!=null&&ad.getLevel()==4))
        {
            throw new AuthorityException();
        }

        teachInfoService.assigne(new TeachInfo(couserCode, couserName, teacherCode, teacherName));
        setCallback("/teachInfo/listAssign.htm");
        return "success";
    }
	
	
	public TeachInfoService getTeachInfoService()
	{
		return teachInfoService;
	}

	public void setTeachInfoService(TeachInfoService teachInfoService)
	{
		this.teachInfoService = teachInfoService;
	}

	public Integer getPageNo()
	{
		return pageNo;
	}

	public void setPageNo(Integer pageNo)
	{
		this.pageNo = pageNo;
	}

	public Page<TeachInfo> getPage()
	{
		return page;
	}

	public void setPage(Page<TeachInfo> page)
	{
		this.page = page;
	}

	public static long getSerialversionuid()
	{
		return serialVersionUID;
	}

	public List<Courses> getCourses()
	{
		return courses;
	}

	public void setCourses(List<Courses> courses)
	{
		this.courses = courses;
	}

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public TeacherService getTeacherService() {
        return teacherService;
    }

    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    public List<Teacher> getTeachers() {
        return teachers;
    }

    public void setTeachers(List<Teacher> teachers) {
        this.teachers = teachers;
    }

    public String getCouserName() {
        return couserName;
    }

    public void setCouserName(String couserName) {
        this.couserName = couserName;
    }

    public String getCouserCode() {
        return couserCode;
    }

    public void setCouserCode(String couserCode) {
        this.couserCode = couserCode;
    }

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public String getTeacherCode() {
        return teacherCode;
    }

    public void setTeacherCode(String teacherCode) {
        this.teacherCode = teacherCode;
    }

	public CacheAble<String, Object> getCacheAble()
	{
		return cacheAble;
	}

	public void setCacheAble(CacheAble<String, Object> cacheAble)
	{
		this.cacheAble = cacheAble;
	}

}
